package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.milk.context.BaseContext;
import com.milk.dto.MilkteaDTO;
import com.milk.dto.MilkteaPageQueryDTO;
import com.milk.entity.Milktea;
import com.milk.entity.MilkteaFlavor;
import com.milk.entity.ShoppingCart;
import com.milk.entity.UserCollect;
import com.milk.mapper.MilkteaFlavorMapper;
import com.milk.mapper.MilkteaMapper;
import com.milk.mapper.ShoppingCartMapper;
import com.milk.mapper.UserCollectMapper;
import com.milk.result.PageResult;
import com.milk.service.IMilkteaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milk.vo.MilkteaVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奶茶 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class MilkteaServiceImpl extends ServiceImpl<MilkteaMapper, Milktea> implements IMilkteaService {

    @Autowired
    private MilkteaMapper milkteaMapper;
    @Autowired
    private MilkteaFlavorMapper milkteaFlavorMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private UserCollectMapper userCollectMapper;

    /**
     * 根据分类id查询商品
     * @param id
     * @return
     */
    public List<MilkteaVO> listCategoryId(Long id) {
        QueryWrapper<Milktea> qw = new QueryWrapper<>();
        qw.eq("category_id",id);
        qw.eq("status",1);
        List<Milktea> list = milkteaMapper.selectList(qw);
        ArrayList<MilkteaVO> arrayList = new ArrayList<>();

        //封装口味信息
        for (Milktea milktea : list) {
            MilkteaVO milkteaVO = new MilkteaVO();
            QueryWrapper<MilkteaFlavor> qw1 = new QueryWrapper<>();
            qw1.eq("milktea_id",milktea.getId());
            MilkteaFlavor milkteaFlavor = milkteaFlavorMapper.selectOne(qw1);
            //查询该用户在购物车中该奶茶的数量
            QueryWrapper<ShoppingCart> shoppingCarqw = new QueryWrapper<>();
            shoppingCarqw.eq("user_id",BaseContext.getCurrentId());
            shoppingCarqw.eq("milktea_id",milktea.getId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCarqw);
            Integer number = 0;
            if (shoppingCarts != null){
                for (ShoppingCart shoppingCart : shoppingCarts) {
                   number += shoppingCart.getNumber();
                }
            }
            milkteaVO.setNumber(number);
            BeanUtils.copyProperties(milktea,milkteaVO);
            milkteaVO.setMilkteaFlavor(milkteaFlavor);
            arrayList.add(milkteaVO);
        }
        return arrayList;
    }

    /**
     * 根据id查询奶茶
     * @param id
     * @return
     */
    public MilkteaVO selectById(Long id) {
        Milktea milktea = milkteaMapper.selectById(id);

        QueryWrapper<MilkteaFlavor> qw = new QueryWrapper<>();
        qw.eq("milkTea_id",id);
        MilkteaFlavor milkteaFlavor = milkteaFlavorMapper.selectOne(qw);

        //查找此用户该奶茶购物车的数量
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> shoppingCartqw = new QueryWrapper<>();
        shoppingCartqw.eq("user_id",userId);
        shoppingCartqw.eq("milktea_id",id);
//        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartqw);
        MilkteaVO milkteaVO = new MilkteaVO();
        BeanUtils.copyProperties(milktea,milkteaVO);
        milkteaVO.setMilkteaFlavor(milkteaFlavor);
//        if (shoppingCart == null){
//            milkteaVO.setNumber(0);
//        }else {
//            milkteaVO.setNumber(shoppingCart.getNumber());
//        }
        QueryWrapper<UserCollect> qw1 = new QueryWrapper<>();
        qw1.eq("user_id",userId);
        qw1.eq("milktea_id",id);
        List<UserCollect> userCollects = userCollectMapper.selectList(qw1);
        List<String> collect = userCollects.stream().map(UserCollect::getMilkteaFlavor).collect(Collectors.toList());
        milkteaVO.setUserCollectList(collect);
        return milkteaVO;
    }

    /**
     * 奶茶分页查询
     * @param dto
     * @return
     */
    public PageResult selectPageQuery(MilkteaPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<MilkteaVO> page = milkteaMapper.pageQUery(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增奶茶
     * @param milkteaDTO
     */
    @Transactional
    public void insertMilktea(MilkteaDTO milkteaDTO) {
        Milktea milktea = new Milktea();
        MilkteaFlavor milkteaFlavor = new MilkteaFlavor();
        BeanUtils.copyProperties(milkteaDTO,milktea);
        BeanUtils.copyProperties(milkteaDTO,milkteaFlavor);


        milktea.setStatus(1);
        milktea.setCreateTime(LocalDateTime.now());
        milktea.setCreateUser(BaseContext.getCurrentId());
        milkteaMapper.insert(milktea);
        milkteaFlavor.setMilkteaId(milktea.getId());
        milkteaFlavorMapper.insert(milkteaFlavor);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        milkteaMapper.deleteByIds(ids);
        QueryWrapper<MilkteaFlavor> qw = new QueryWrapper<>();
        qw.in("milkTea_id",ids);
        milkteaFlavorMapper.delete(qw);
    }

    /**
     * 更新奶茶信息
     * @param milkteaDTO
     */
    public void updataMilktea(MilkteaDTO milkteaDTO) {
        Milktea milktea = new Milktea();
        MilkteaFlavor milkteaFlavor = new MilkteaFlavor();
        BeanUtils.copyProperties(milkteaDTO,milktea);
        BeanUtils.copyProperties(milkteaDTO,milkteaFlavor);

        milktea.setUpdateTime(LocalDateTime.now());
        milktea.setUpdateUser(BaseContext.getCurrentId());
        milkteaMapper.updateById(milktea);

        if (milkteaDTO.getSweetness()!=null || milkteaDTO.getTemperature()!=null){
            QueryWrapper<MilkteaFlavor> qw = new QueryWrapper<>();
            qw.eq("milkTea_id",milkteaDTO.getId());
            milkteaFlavorMapper.update(milkteaFlavor,qw);
        }

    }

    /**
     * 首页展示奶茶新品
     * @return
     */
    public List<Milktea> selectNew() {
        QueryWrapper<Milktea> qw = new QueryWrapper<>();
        qw.eq("status",1);
        qw.orderByDesc("create_time");
        List<Milktea> list = milkteaMapper.selectList(qw);

        List<Milktea> milkteas = new ArrayList<>();
        milkteas.add(list.get(0));
        milkteas.add(list.get(1));
        return milkteas;
    }


}
